import re


if __name__ == '__main__':
    def2word,word2def=dict(),dict()
    words=[]
    with open(r'Def2word.txt','r',encoding='utf-8') as ref:
        content=ref.readlines()
        print('len(content):', len(content))
        for i in range(len(content)):
            ret=re.search('^\#(.+)',content[i].strip())
            if ret:
                # key=ret.group(1).split('|')[1]
                key=ret.group(1).strip().split('|')[1]
            else:
                def2word.setdefault(key,[]).append(content[i].strip().split()[0])
                word2def.setdefault(content[i].strip().split()[0],[]).append(key)
    
    print(word2def['稀'])
    print(def2word['少'][:3])
    import pickle
    with open("final/word2def.pkl",'wb') as out_file:
        pickle.dump(word2def,out_file) 
    with open("final/def2word.pkl",'wb') as out_file:
        pickle.dump(def2word,out_file) 

    '''
    .manner_ActGeneral|泛动
    cautious|慎 AV
    '''
    # 处理Event2RoleActor.txt:义原_义原：角色
    Event2RoleActor=dict()
    with open(r'Event2RoleActor.txt','r',encoding='utf-8') as ref:
        content=ref.readlines()
        print("len(content):", len(content))
        for i in range(len(content)):
            ret=re.search('^\.(.+)',content[i].strip())
            if ret:
                role,v_sememe = ret.group(1).split('_')
                v_sememe = v_sememe.split('|')[1]
            else:
                key = v_sememe + '_' + content[i].strip().split()[0].split('|')[1]
                Event2RoleActor.setdefault(key,[]).append(role)
    print(Event2RoleActor['定向_标志'])  # ['instrument']
    print("len(Event2RoleActor):",len(Event2RoleActor))
    with open("final/vnsememe_role.pkl",'wb') as out_file:
        pickle.dump(Event2RoleActor,out_file) 
    # 3
    print("len(def2word):", len(def2word))  # 1796
    v_sememe = set()
    n_sememe = set()
    for item in Event2RoleActor:
        v_sememe.add(item.split('_')[0])
        n_sememe.add(item.split('_')[1])
    print("len(v_sememe):",len(v_sememe))
    print("len(n_sememe):",len(n_sememe))
    chaji = list(v_sememe.difference(set(def2word)))
    print("义原没有匹配到对应的def2word", chaji)  # 6个义原没有匹配到对应的def2word
    print("全部的词,len(word2def):",len(word2def))
    all_sememe = v_sememe|n_sememe
    for item in def2word:
        all_sememe.add(item)
    print("全部义原all_sememe:",len(all_sememe))
    print("len(word2def):",len(word2def))
    multi = []
    s = set()
    for item in Event2RoleActor:  # 统计有两种以上角色关系的义原
        if len(Event2RoleActor[item]) >= 2:
            multi.append(item+'_' + str(len(Event2RoleActor[item])))
            s.add(len(Event2RoleActor[item]))
        if len(Event2RoleActor[item]) == 6:
            print('有6种义原关系的词对:', item)
            print('6种角色关系为:',Event2RoleActor[item]) 
    single = []  # 义原写入文件，多字词上，单字下
    with open('sememe.txt', 'w', encoding = 'utf-8') as file:
        for item in all_sememe:
            if len(item) >= 2:
                file.write(item + '\n')
            else:
                single.append(item)
    with open('sememe.txt', 'a+', encoding = 'utf-8') as file:
        for item in single:
            file.write(item + '\n')






